package com.bdq.search.api.controller;

import cn.hutool.core.convert.Convert;
import com.bdq.common.entity.common.CommonResult;
import com.bdq.common.entity.common.PageResult;
import com.bdq.common.feign.content.vo.ContentListVO;
import com.bdq.search.api.model.param.SearchHistoryDeleteParam;
import com.bdq.search.api.model.param.SearchPageParam;
import com.bdq.search.api.model.vo.SearchHistoryVO;
import com.bdq.search.application.model.cmd.SearchHistoryDeleteCmd;
import com.bdq.search.application.model.dto.SearchHistoryAppDTO;
import com.bdq.search.application.model.query.SearchPageQry;
import com.bdq.search.application.service.SearchQueryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * bdq-search搜索相关
 *
 * @author malei
 * @date 2024-05-26 16:34
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Resource
    private SearchQueryService searchQueryService;

    /**
     * 搜索分页
     *
     * @param param
     * @return
     */
    @GetMapping("/page")
    public CommonResult<PageResult<ContentListVO>> searchPage(@Validated SearchPageParam param){
        return CommonResult.success(searchQueryService.searchPage(Convert.convert(SearchPageQry.class, param)));
    }

    /**
     * 搜索历史
     *
     * @param
     * @return
     */
    @GetMapping("/history")
    public CommonResult<List<SearchHistoryVO>> searchHistory(){
        List<SearchHistoryAppDTO> appDTOList = searchQueryService.searchHistory();
        return CommonResult.success(Convert.toList(SearchHistoryVO.class, appDTOList));
    }

    /**
     * 删除搜索记录
     *
     * @param
     * @return
     */
    @PostMapping("/history/delete")
    public CommonResult<Boolean> searchHistoryDelete(@RequestBody SearchHistoryDeleteParam param){
        searchQueryService.searchHistoryDelete(Convert.convert(SearchHistoryDeleteCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 搜索历史清除
     *
     * @param
     * @return
     */
    @GetMapping("/history/clear")
    public CommonResult<Boolean> searchHistoryClear(){
        searchQueryService.searchHistoryClear();
        return CommonResult.success(true);
    }
}
